<?php
class DeviceParser{
    const SERVER_NOT_RESPONDING = -1;
    
    
    public function ParseVen($fname){
        $handle = fopen($fname,'r');

        $i = 0;
        while (!feof($handle)) {
            $buf = fgets($handle);
            $res = explode('|', $buf);
            if(count($res)>1){
                $type = substr($res[0],0,1);
                $arr[$i] = $res;
                $arr[$i][0] = substr($arr[$i][0],1,6);
                $ar1 = explode('=', $res[13]);
                if($type=='M' || $type=='R' ||$type=='A' ||$type=='V' ||$type=='1' ||$type=='C'){
                    $arr[$i][13] = 'none';
                    $arr[$i][14] = $ar1[0];
                }else{
                    $ids = explode(':',$ar1[0]);
                    $arr[$i][13] = $ids[0];
                    $arr[$i][14] = $ids[1];
                }
                $arr[$i][15] = $ar1[1];
                $str = implode('  ',$arr[$i]);
                $i++;
            }
        }
        fclose($handle);
        return $arr;
    }
    public function ParseDrv($fname)
    {
     $array=array();
     $base=fopen($fname,"r");
     $category=array('0'=>'graphics','1'=>'mainboard','2'=>'storage','3'=>'raid','4'=>'audio','5'=>'scanner','6'=>'printer','7'=>'modem','8'=>'network','9'=>'input','10'=>'cpu','11'=>'bluetooth','12'=>'portable','13'=>'camera');
     while (!feof ($base)) 
        {
          $str=explode("|",$buffer = fgets($base));
          @$str[1]=$category[$str[1]];
          $array[]=$str;
        }
    fclose ($base);
    return $array;
    }

    public function SaveToDB($array=array(),$param){
        $dblocation = "santa";
        $dbuser = "root";
        $dbpasswd = "312m16";
        $dbname="devs";
        mysql_connect($dblocation,$dbuser,$dbpasswd);
        mysql_select_db($dbname);
        if($param=="ven"){$categories=array('win95','win98','2k','xp32','xp64','vistax32','2003x32','2003x64','vistax64','2008x32','2008x64','7x32','7x64','vendor','device','name');}
        if($param=="drv"){$categories=array(0=>'downnum',1=>'category',2=>'fname',3=>'fsize',4=>'test2',5=>'drivername',6=>'company',7=>'fdate',8=>'ver2',9=>'test',10=>'fhash',11=>'alink',12=>'str1',13=>'str2',14=>'str3',15=>'str4',16=>'str5',17=>'str6',18=>'str7',19=>'str8',20=>'str9',21=>'str10',22=>'str11',23=>'str12',24=>'str13');}
        
        foreach($array as $i=>$str){
            for($j = 0;$j<count($str);$j++){
                $str[$j] = "'".$str[$j]."'";
            }
            $values = implode(', ',$str);
            $cats = implode(', ',$categories);
            if($param=="ven"){$query = 'INSERT INTO devids('.$cats.') VALUES('.$values.')';}
            if($param=="drv"){$query = 'INSERT INTO drivers('.$cats.') VALUES('.$values.')';}
            mysql_query($query);
        }
    }
    
    private function ConnectToDB(){
        $dblocation = "santa";
        $dbuser = "root";
        $dbpasswd = "312m16";
        $dbname="devs";
        
        $dbconn = @mysql_connect($dblocation,$dbuser,$dbpasswd);
        if (!$dbconn) {
            echo("<p>Sorry but DB Server not responding.</p>");
            return SERVER_NOT_RESPONDING;
        }
        
        if (!@mysql_select_db($dbname, $dbconn)){
            echo( "<p>Sorry but DB Server not responding.</p>" );
            return SERVER_NOT_RESPONDING;
        }
        return $dbconn;
    }
    
    public function MakeFilelist(){
        $server = "http://91.121.59.203/";
        $file = fopen('list.txt','w+');
        $conn = $this->ConnectToDB();
        $query = 'SELECT category, fname FROM drivers';
        $result = mysql_query($query,$conn);
        while($row = mysql_fetch_row($result)){
            fwrite($file,$server.$row[0].'/'.$row[1]."\n");
        }
        fclose($file);
    }
}

?>